package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.query.Queries;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.common.security.util.SecurityUtils;
import com.engine4cloud.tdf.supervision.convert.SupervisionLevelSettingConvert;
import com.engine4cloud.tdf.supervision.dto.create.SupervisionLevelSettingCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.SupervisionLevelSettingQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.SupervisionLevelSettingResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.SupervisionLevelSettingUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.SupervisionLevelSettingEntity;
import com.engine4cloud.tdf.supervision.mapper.SupervisionLevelSettingMapper;
import com.engine4cloud.tdf.supervision.service.SupervisionLevelSettingService;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * 监管级别配置表
 *
 * @author wenjie
 * @date 2024-09-24 15:53:24
 */
@Service
public class SupervisionLevelSettingServiceImpl extends ServiceImpl<SupervisionLevelSettingMapper, SupervisionLevelSettingEntity> implements SupervisionLevelSettingService {
    @Override
    public PageInfo<SupervisionLevelSettingResultDTO> pageList(Params pageable, SupervisionLevelSettingQueryDTO dto) {
        Page<SupervisionLevelSettingEntity> page = page(Pages.page(pageable), wrapper(dto));
        return Pages.convert(page, SupervisionLevelSettingConvert.INSTANCE::convert);
    }

    @Override
    public SupervisionLevelSettingResultDTO getById(Long id) {
        return SupervisionLevelSettingConvert.INSTANCE.convert(super.getById(id));
    }

    @Override
    public Boolean create(SupervisionLevelSettingCreateDTO dto) {
        return super.save(SupervisionLevelSettingConvert.INSTANCE.create(dto, SecurityUtils.getUser().getTenantId()));

    }

    @Override
    public Boolean update(SupervisionLevelSettingUpdateDTO dto) {
        Optional.ofNullable(super.getById(dto.getId()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        return super.updateById(SupervisionLevelSettingConvert.INSTANCE.update(dto));
    }

    private LambdaQueryWrapper<SupervisionLevelSettingEntity> wrapper(SupervisionLevelSettingQueryDTO query) {
        LambdaQueryWrapper<SupervisionLevelSettingEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(SupervisionLevelSettingEntity::getId, v));
        Queries.accept(query.getCode(), v -> wrapper.eq(SupervisionLevelSettingEntity::getCode, v));
        Queries.accept(query.getName(), v -> wrapper.like(SupervisionLevelSettingEntity::getName, v));
        return wrapper;
    }
}